
module WindowDefineNewHaircut (
    windowDefineNewHaircutAcid
    ) where

import Graphics.UI.Gtk
import Data.IORef
import MainHandler
import Haircut
import WindowUtils

import Data.Acid
import Data.SafeCopy
import Data.Typeable


    
--windowDefineNewHaircutAcid :: IO (AcidState st) -> IO ()
windowDefineNewHaircutAcid state = do
    initGUI
    window <- windowNew
    windowSetPosition window WinPosCenter
    windowSetTitle window "Definir nuevo corte"
    
    (label1,frameCortesGenerales) <- myLabelWithFrameNew
    labelSetText label1 "Cortes Generales"
    (label2,frameNuevoCorte) <- myLabelWithFrameNew
    labelSetText label2 "Nuevo Corte"
    labelHcName <- labelNew (Just "nombre:")
    labelHcDesc <- labelNew (Just "descripcion:")
    
    buttonAdd <- buttonNewWithLabel "Agregar nuevo corte"
    buttonDel <- buttonNewWithLabel "Eliminar corte"
    volverButton <- buttonNewWithLabel "Volver"
    volverButton `onClicked` (widgetDestroy window)

    entryName <- entryNew
    entryDescription <- entryNew
    
    buttonAdd `onClicked` addHaircutAcid state entryName entryDescription window

    combo <- comboHaircutsAcid state
    buttonDel `onClicked` deleteHaircutAcid state combo window    
    
    sep <- hSeparatorNew
    
    vbox <- vBoxNew False 5
    
    boxPackStart vbox frameCortesGenerales PackNatural 1
    boxPackStart vbox combo PackNatural 1
    boxPackStart vbox buttonDel PackNatural 1
    boxPackStart vbox sep PackNatural 1
    boxPackStart vbox frameNuevoCorte PackNatural 1
    boxPackStart vbox labelHcName PackNatural 1
    boxPackStart vbox entryName PackNatural 1
    boxPackStart vbox labelHcDesc PackNatural 1
    boxPackStart vbox entryDescription PackNatural 1
    boxPackStart vbox labelHcDesc PackNatural 1
    boxPackStart vbox buttonAdd PackNatural 1
    boxPackStart vbox volverButton PackNatural 1
    
    set window [windowDefaultWidth := 300, windowDefaultHeight := 300,
              containerBorderWidth := 10, containerChild := vbox]
              
    widgetShowAll window




------------------------ FUNCIONES CONTROL ACID -------------------------------
------------------------------------------------------------------------------

comboHaircutsAcid state = do
 
    haircuts <- getHaircutsAcid state
    store <- listStoreNew (haircuts)
    combo <- comboBoxEntryNewWithModel store
    customStoreSetColumn store textColumn (getHaircutName)
    comboBoxEntrySetTextColumn combo textColumn
  
    return combo
    
-- addHaircutAcid :: IO (AcidState st) -> 
addHaircutAcid state entryName entryDesc win = do
    name <- entryGetText entryName
    description <- entryGetText entryDesc
    
    let newHc = createHaircut name description
    addNewHaircutAcid state newHc
    addMsg <- messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte agregado"
    dialogRun addMsg
    
    widgetDestroy win
    
deleteHaircutAcid state combo win = do
    index <- comboBoxGetActive combo
    if (index < 0) 
     then return ()
     else do
            -- TODO (modifyIORef db (deleteHcFromDb index))
            addMsg <- (messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte borrado")
            dialogRun addMsg
            widgetDestroy win 
            return ()

--------------------------------------------------------------------------
----------------------FUNCIONES DE CONTROL--------------------------------
--------------------------------------------------------------------------





textColumn :: ColumnId Haircut String
textColumn = makeColumnIdString 0

 
 
-- TODO

    
delHaircut 0 (hc:hcs) = hcs
delHaircut n (hc:hcs) = hc : (delHaircut (n-1) hcs)
 

    
    
    